package com.wangyi.pet.service.impl;

import com.sun.org.apache.bcel.internal.generic.IFEQ;
import com.wangyi.basic.exception.BusinessException;
import com.wangyi.basic.service.IBaiduAiAuditService;
import com.wangyi.basic.util.LoginContext;
import com.wangyi.org.domain.Employee;
import com.wangyi.org.mapper.EmployeeMapper;
import com.wangyi.pet.domain.Pet;
import com.wangyi.pet.domain.PetDetail;
import com.wangyi.pet.domain.PetOnlineAuditLog;
import com.wangyi.pet.mapper.PetDetailMapper;
import com.wangyi.pet.mapper.PetMapper;
import com.wangyi.pet.mapper.PetOnlineAuditLogMapper;
import com.wangyi.pet.service.IPetService;
import com.wangyi.basic.service.impl.BaseServiceImpl;
import com.wangyi.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Transactional
    @Override
    public void add(Pet pet) {
        super.add(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.save(petDetail);
        }
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null) {
            petDetail.setPetId(pet.getId());
            petDetailMapper.updatePetDetailByPetId(petDetail);
        }
    }

    @Transactional
    @Override
    public void del(Long id) {
        super.del(id);
        petDetailMapper.delPetDetailByPetId(id);
    }

    /**
     * 宠物上架
     * @param ids
     * @param request 接收当前登录人信息，用来修改宠物上下架日志表数据
     */
    @Override
    public void onsale(Long[] ids, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLoginUser(request);
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());

        for (Long id : ids) {
            Pet pet = petMapper.loadById(id);
            // 1.上架之前对文本和图片做审核
            Boolean textBoo = baiduAiAuditService.textAudit(pet.getName());
            Boolean imageBoo = baiduAiAuditService.imageAudit(pet.getResources());
            if (textBoo && imageBoo) {
                // 2.审核通过：修改状态 + 添加上架的审核成功日志
                pet.setOnsaletime(new Date());
                pet.setState(1);
                pet.setOffsaletime(null);
                petMapper.update(pet);

                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                log.setState(1);// 1审核通过  0待审核  -1驳回
                log.setAuditTime(new Date());
                log.setNote("审核通过，上架");
                log.setAuditId(employee.getId());
                petOnlineAuditLogMapper.save(log);
            } else {
                // 3.审核失败：添加上架的审核失败日志
                PetOnlineAuditLog log = new PetOnlineAuditLog();
                log.setPetId(pet.getId());
                log.setState(-1);//1审核通过  0待审核  -1驳回
                log.setAuditTime(new Date());
                log.setNote("审核失败，内容有非法数据");
                log.setAuditId(employee.getId());
                petOnlineAuditLogMapper.save(log);
                throw new BusinessException("审核失败！！！内容有非法数据");
            }
        }
    }


    /**
     * 宠物下架
     * @param ids
     */
    @Override
    public void offsale(Long[] ids) {
        Date offsaletime = new Date();
        petMapper.offsale(ids, offsaletime);
    }
}
